var mjfeed = {
	base: "",
	locale: "zh_CN"
};

(function($) {
	// 检测登录
	$.checkLogin = function() {
		var result = false;
		$.ajax({
			url: mjfeed.base + "/login/check.shtml",
			type: "GET",
			dataType: "json",
			cache: false,
			async: false,
			success: function(data) {
				result = data;
			}
		});
		return result;
	}

	// 跳转登录
	$.redirectLogin = function (redirectUrl, message) {
		var href = mjfeed.base + "/login.shtml";
		if (redirectUrl != null) {
			href += "?redirectUrl=" + encodeURIComponent(redirectUrl);
		}
		if (message != null) {
			alert(message);
			setTimeout(function() {
				location.href = href;
			}, 1000);
		} else {
			location.href = href;
		}
	}
})(jQuery);


var basePath = "";
$().ready(function(){
	basePath = $("#basePath").val()
	if ($.tools != null) {
		var $tab = $("#tab");
		// Tab效果
		$tab.tabs("table.tabContent, div.tabContent");
	}
	
	// 编辑器
	if(typeof(KindEditor) != "undefined") {
		KindEditor.ready(function(K) {
			editor = K.create(".editor", {
				items: [
					"source", "|", "undo", "redo", "|", "preview", "template", "cut", "copy", "paste",
					"plainpaste", "wordpaste", "|", "justifyleft", "justifycenter", "justifyright",
					"justifyfull", "insertorderedlist", "insertunorderedlist", "indent", "outdent", "subscript",
					"superscript", "clearhtml", "quickformat", "selectall", "|", "fullscreen", "/",
					"formatblock", "fontname", "fontsize", "|", "forecolor", "hilitecolor", "bold",
					"italic", "underline", "strikethrough", "lineheight", "removeformat", "|", "image",
					"flash", "media", "table", "hr", "emoticons", "baidumap", "pagebreak",
					"anchor", "link", "unlink"
				],
				autoHeightMode : true,
				uploadJson : basePath+"kindeditor/upload.shtml",
				afterCreate : function() {
					this.loadPlugin('autoheight');
				},
				afterChange: function() {
					this.sync();
				}
			});
		});
	}
	
	//顶部 是否登陆
	$headerLogin = $("#headerLogin");
	$headerRegister = $("#headerRegister");
	$headerShownameLi = $("#headerShownameLi");
	$headerShowname = $("#headerShowname");
	$headerLogout = $("#headerLogout");
	$loginmask = $("#login-mask");
	if ($.checkLogin()) {
		var username = getCookie("username");
		var showname = getCookie("name");
		if (username != null) {
			$headerShowname.text(showname);
			$headerShownameLi.show();
			$headerLogout.show();
			$loginmask.hide();
		} else {
			$headerLogin.show();
			$headerRegister.show();
			$loginmask.show();
		}	
	} else {
		$headerLogin.show();
		$headerRegister.show();
		$loginmask.show();
	}
		
	
	//商品搜索
	$("#img_search").click(function(){
		var keyword = $("#keyword_search").val();
		if(keyword != null && keyword != "") {
			window.location.href=basePath+"shop/product/list.shtml?keyword="+encodeURI(keyword);
		}
	});
	
	//初始化购物车数量
	$cartnum = $("#cartnum");
	$.ajax({
		type : "get",
		url : basePath+"cart/getCart.shtml",
		dataType : "json",
		cache: false,
		success: function(data){
			var success = data.success;
			if (success == "true") {
				var num = data.quantity;
				$cartnum.text(num);
			}
		},
		error :function() {
		}
	});
});

//添加购物车
function addCar(productId, quantity) {
	$cartnum = $("#cartnum");
	var num = parseInt($cartnum.text());
	var quantity = (quantity == null || quantity == "") ? 1 : quantity;
	$.ajax({
		type : "post",
		url : basePath+"cart/add.shtml",
		data : {"id":productId , "quantity":quantity },
		dataType : "json",
		cache: false,
		success: function(data){
			var success = data.success;
			if (success == "true") {
				alert("添加成功");
				num += parseInt(quantity);
				$cartnum.text(num);
			} else {
				alert("添加失败 "+data.message);
			}
		},
		error :function() {
		}
	});
}

//去购物车
function go2Car() {
	window.location.href=basePath+"cart/list.shtml";
}

//添加关注
function addFavorite(productId) {
	if (!$.checkLogin()) {
		$.redirectLogin(basePath+"product.html");
		return false;
	} else {
		$.ajax({
			type : "post",
			url : basePath+"member/favorite/add.shtml",
			data : {"productId":productId },
			dataType : "json",
			cache: false,
			success: function(data){
				var success = data.success;
				if (success == "true") {
					alert("添加成功");
				} else {
					alert("添加失败 "+data.message);
				}
			},
			error :function() {
			}
		});
	}
}

//添加Cookie
function addCookie(name, value, options) {
	if (arguments.length > 1 && name != null) {
		if (options == null) {
			options = {};
		}
		if (value == null) {
			options.expires = -1;
		}
		if (typeof options.expires == "number") {
			var time = options.expires;
			var expires = options.expires = new Date();
			expires.setTime(expires.getTime() + time * 1000);
		}
		document.cookie = encodeURIComponent(String(name)) + "=" + encodeURIComponent(String(value)) + (options.expires ? "; expires=" + options.expires.toUTCString() : "") + (options.path ? "; path=" + options.path : "") + (options.domain ? "; domain=" + options.domain : ""), (options.secure ? "; secure" : "");
	}
}

// 获取Cookie
function getCookie(name) {
	if (name != null) {
		var value = new RegExp("(?:^|; )" + encodeURIComponent(String(name)) + "=([^;]*)").exec(document.cookie);
		return value ? decodeURIComponent(value[1]) : null;
	}
}

// 移除Cookie
function removeCookie(name, options) {
	addCookie(name, null, options);
}

// 百度统计
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?9bf78ebe7d3534adb2d0c866a6ef16b4";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();